package com.xiaochong.tir.manager.controller;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.Navigation;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.NavigationService;
import com.xiaochong.tir.manager.util.ManagerVersion;

import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 菜单管理
 *
 * @author 刘晓阳
 * @create 2017-12-28 19:58
 **/
@Controller
@RequestMapping(ManagerVersion.API_V1_PREFIX+"/navigation")
@Api("菜单管理")
public class NavigationController {

    @Autowired
    private NavigationService navigationService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 目录页面
     *
     * @return
     */
    @RequestMapping(value = "/parentNavList", method = RequestMethod.GET)
    public String parentNavList(){
        return "/navigation/parentNavList";
    }

    /**
     * 子菜单页面
     *
     * @return
     */
    @RequestMapping(value = "/childNavList", method = RequestMethod.GET)
    public String childNavList(@RequestParam("parentId") Integer parentId,@RequestParam(value="parentName",required=false) String parentName, Map<String, Object> map){
    	map.put("parentName", parentName);
        map.put("parentId", parentId);
        return "/navigation/childNavList";
    }

    /**
     * 添加目录菜单页面
     *
     * @return
     */
    @RequestMapping(value = "/addNavigation", method = RequestMethod.GET)
    public String addNavigation(){
        return "/navigation/addNavigation";
    }

    /**
     * 添加子菜单页面
     *
     * @return
     */
    @RequestMapping(value = "/addChildNavigation", method = RequestMethod.GET)
    public String addChildNavigation(@RequestParam("parentId") Integer parentId, Map<String, Object> map){

        map.put("parentId", parentId);
        return "/navigation/addChildNavigation";
    }

    /**
     * 添加目录菜单
     *
     * @return
     */
    @ApiOperation(value = "添加菜单",notes = "添加菜单")
    @RequestMapping(value = "/addMenu", method = RequestMethod.GET)
    public String addNavigation(HttpServletRequest request, Map<String, Object> map) {
        redisServer.set(RedisKeysUtils.LOGINNAME,"添加菜单");
        String title = request.getParameter("title");
        String href = request.getParameter("href");
        String rank = request.getParameter("rank");
        String icon = request.getParameter("icon");

        try {
            Navigation navigation = new Navigation();
            navigation.setTitle(title);
            navigation.setHref(href);
            Navigation n = navigationService.selectByEntity(navigation);
            if(null != n){
                map.put("msg", "重复添加");
                return "/navigation/parentNavList";
            }
            if (StringUtils.isNotEmpty(rank)) {
                navigation.setRank(Integer.valueOf(rank));
            }
            navigation.setSpread(false);
            navigation.setIcon(icon);
            navigation.setStatus(1);
            navigation.setCreateTime(new Date());
            int id = navigationService.addNavigation(navigation);
            map.put("msg", "添加成功");

            return "/navigation/parentNavList";
        } catch (Exception e) {
            LogTrace.error(getClass().getSimpleName() + "/addNavigation", e.getMessage(), e);
            return "/navigation/addNavigation";
        }
    }

    /**
     * 添加目录菜单
     *
     * @return
     */
    @ApiOperation(value = "添加子菜单",notes = "添加子菜单")
    @RequestMapping(value = "/addChildMenu", method = RequestMethod.GET)
    public String addChildMenu(HttpServletRequest request, Map<String, Object> map) {
        redisServer.set(RedisKeysUtils.LOGINNAME,"添加子菜单");
        String title = request.getParameter("title");
        String parentId = request.getParameter("parentId");
        String href = request.getParameter("href");
        String rank = request.getParameter("rank");
        String icon = request.getParameter("icon");

        try {
            Navigation navigation = new Navigation();
            navigation.setTitle(title);
            navigation.setHref(href);
            if (StringUtils.isNotEmpty(parentId)) {
                navigation.setParentId(Integer.parseInt(parentId));
            }
            Navigation n =  navigationService.selectByEntity(navigation);
            if(null != n){
                map.put("msg", "重复添加");
                return "/navigation/parentNavList";
            }
            navigation.setSpread(false);
            if (StringUtils.isNotEmpty(rank)) {
                navigation.setRank(Integer.valueOf(rank));
            }
            navigation.setIcon(icon);
            navigation.setStatus(1);
            navigation.setCreateTime(new Date());
            int id = navigationService.addNavigation(navigation);
            map.put("msg", "添加成功");

            return "/navigation/parentNavList";
        } catch (Exception e) {
            LogTrace.error(getClass().getSimpleName() + "/addChildMenu", e.getMessage(), e);
            return "/navigation/addChildNavigation";
        }
    }
}
